package cc.booku.owlbox.TestPage;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.R;
import cc.booku.owlbox.RsaUtil.Decrypt;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    // List<Bean> data = new ArrayList<>();
    private List<CustomizeDomestic_C> customizeCList = new ArrayList<CustomizeDomestic_C>();
    private String CUSTOMIZE_DOMESTIC = "国产";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mRecyclerView = findViewById(R.id.recyclerview);
        //生命为瀑布流的布局方式，3列，布局方向为垂直
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        //解决item跳动
        //manager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        mRecyclerView.setLayoutManager(manager);
        // initData();
        String ConfigFiles_json = FileoOperations.readAssetsTxt(RecyclerViewActivity.this, "TxtData/ConfigFiles");
        JSONObject objsd = JSONObject.parseObject(ConfigFiles_json);
        String ciphertext = objsd.getString("ADDRESSCIPHER");//地址密文
        String DecryptionAddress = Decrypt.RsaDecrypt(RecyclerViewActivity.this, ciphertext);
        String typeseek = objsd.getString("TYPESEEK");//类型检索
        //国产
        init_CustomizeDomestic(DecryptionAddress + typeseek + CUSTOMIZE_DOMESTIC);
        //MyAdapter adapter = new MyAdapter(RecyclerViewActivity.this, data);
        //mRecyclerView.setAdapter(adapter);


    }

//    private void initData() {
//        Bean bean;
//        for (int i = 0; i < 10; i++) {
//            bean = new Bean("美女" + i, R.mipmap.ic_launcher);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.tab_loading);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.tab_loading);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.tab_loading);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.ic_launcher);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.tab_loading);
//            data.add(bean);
//            bean = new Bean("美女" + i, R.mipmap.ic_launcher);
//            data.add(bean);
//        }
//    }


    private Handler Thandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case 22:

                    CustomizeVideoData((ArrayList<CustomizeDomestic_C>) customizeCList);
                    break;


            }
        }
    };

    private CustomizeDomestic_C customizeC;

    private void init_CustomizeDomestic(String url) {

        customizeCList.clear();

        OkHttpClient client = new OkHttpClient();
        Request request = new Request
                .Builder()
                .url(url)//要访问的链接
                .build();

        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();

//                String jsonstr1 = res.replace("[{", "{");
//                String jsonstr2 = jsonstr1.replace("}]", "}");


//                Message msg = new Message();
//                msg.what = 16;
//                Bundle bundle = new Bundle();
//                bundle.putString("jsondata",res);  //往Bundle中存放数据
//                // bundle.putString("text2","Time：2016-11-03");  //往Bundle中put数据
//                msg.setData(bundle);//mes利用Bundle传递数据
//                handler.sendMessage(msg);//用activity中的handler发送消息

                try {
                    List<CustomizeDomestic_C> collectionStationCList = JSON.parseArray(res, CustomizeDomestic_C.class);
                    for (CustomizeDomestic_C normVideo : collectionStationCList) {
                        customizeC = new CustomizeDomestic_C();

//    vod_score  ---评分
//    vod_name	----影片名称
//    vod_class  ----类型
//    vod_pic ----图片地址
//    vod_play_url --视频地址
//    vod_add_time  ---添加日期

//    vod_actor ---演员
//    vod_director ---导演
//    vod_blurb  ---影片简介
//    vod_area  ---地区
//    vod_year  ---上映年份
//    vod_lang  ---语言

                        String vod_score = normVideo.getVod_score();
                        String vod_name = normVideo.getVod_name();
                        String vod_class = normVideo.getVod_class();
                        String vod_add_time = normVideo.getVod_add_time();
                        String vod_pic = normVideo.getVod_pic();
                        String vod_play_url = normVideo.getVod_play_url();

                        String vod_actor = normVideo.getVod_actor();
                        String vod_director = normVideo.getVod_director();
                        String vod_blurb = normVideo.getVod_blurb();
                        String vod_area = normVideo.getVod_area();
                        String vod_year = normVideo.getVod_year();
                        String vod_lang = normVideo.getVod_lang();
                        String vod_id = normVideo.getVod_id();

                        // Log.d("ImageviewHolder", "显示图片url======" + vod_pic);

                        customizeC.setVod_score("评分:" + vod_score);
                        customizeC.setVod_name(vod_name);
                        customizeC.setVod_class(vod_class + "#" + vod_year + "#" + vod_area + "#" + vod_lang + "#" + vod_id);
                        customizeC.setVod_add_time(vod_add_time);
                        customizeC.setVod_pic(vod_pic);
                        customizeC.setVod_pic_url(vod_pic);
                        customizeC.setVod_play_url(vod_play_url);
                        customizeC.setVod_value_add("导演：" + vod_director + "\n演员：" + vod_actor + "\n类型：" + vod_class + "\n上映地区：" + vod_area + "\n上映年份：" + vod_year + "\n语言：" + vod_lang + "\n$片名：" + vod_name + "\n剧情简介：" + vod_blurb);


                        customizeCList.add(customizeC);
                    }

                } catch (Exception e) {

                    Log.i("onResponse===========>", res);
//异常处理
                }


                Message msg = new Message();
                msg.what = 22;
                Thandler.sendMessage(msg);//用activity中的handler发送消息
            }
        });

    }

    private CustomizeDomestic_A customizeA;

    public void CustomizeVideoData(ArrayList<CustomizeDomestic_C> customizeCS) {


        customizeA = new CustomizeDomestic_A(RecyclerViewActivity.this, customizeCS);

        mRecyclerView.setAdapter(customizeA);
//        mRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//
//                //Toast.makeText(getContext(), "Click ++ item" + i, Toast.LENGTH_SHORT).show();
//                final TextView vod_class1 = (TextView) view.findViewById(R.id.vod_class);
//                final TextView vod_score1 = (TextView) view.findViewById(R.id.vod_score);
//                final TextView vod_name1 = (TextView) view.findViewById(R.id.vod_name);
//                final TextView vod_add_time1 = (TextView) view.findViewById(R.id.vod_add_time);
//                final TextView vod_play_url1 = (TextView) view.findViewById(R.id.vod_play_url);
//                final TextView vod_value_add1 = (TextView) view.findViewById(R.id.vod_value_add);
//                final TextView vod_pic_url1 = (TextView) view.findViewById(R.id.vod_pic_url);
//
//                String vod_class2 = vod_class1.getText().toString();
//                String vod_score2 = vod_score1.getText().toString();
//                String vod_name2 = vod_name1.getText().toString();
//                String vod_add_time2 = vod_add_time1.getText().toString();
//                String vod_play_url2 = vod_play_url1.getText().toString();
//                String vod_value_add2 = vod_value_add1.getText().toString();
//                String vod_pic_url2 = vod_pic_url1.getText().toString();
//
//                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putString("vod_class", vod_class2);
//                bundle.putString("vod_score", vod_score2);
//                bundle.putString("vod_name", vod_name2);
//                bundle.putString("vod_add_time", vod_add_time2);
//                bundle.putString("vod_play_url", vod_play_url2);
//                bundle.putString("vod_value_add", vod_value_add2);
//                bundle.putString("vod_pic", vod_pic_url2);
//                intent.putExtras(bundle);
//                intent.setClass(RecyclerViewActivity.this, PlayerActivity.class);
//                startActivity(intent);
//
//
//            }
//        });
//
    }


}
